using System.Windows.Forms;

namespace EIM_Accounting
{
    /// <summary>
    /// 合同 合同管理 合同费用编辑
    /// </summary>
    public partial class CP010103 : Form
    {
        BLL.CP010103 cp010103 = new BLL.CP010103();

        public CP010103()
        {
            InitializeComponent();
        }

        private void input_float_Press(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }

            if (e.KeyChar == 46)                       //小数点
            {
                string txtValue = ((System.Windows.Forms.TextBox)sender).Text;

                if (txtValue.Length <= 0)
                    e.Handled = true;           //小数点不能在第一位
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(txtValue, out oldf);
                    b2 = float.TryParse(txtValue + e.KeyChar.ToString(), out f);
                    if (b2 == true)
                    {
                        if (b1 == false)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (comboBox1.SelectedValue != null)
            {
                label3.Text = cp010103.getContractInfobyId(comboBox1.SelectedValue.ToString());
            }
        }

        private void CP010103_Load(object sender, System.EventArgs e)
        {
            comboBox1.DataSource = cp010103.getContractList().Tables[0];

            comboBox1.DisplayMember = "SignComp";
            comboBox1.ValueMember = "ID";

            if (comboBox1.Items.Count < 1)
            {
                btnSave.Enabled = false;
            }
            else
            {
                label3.Text = cp010103.getContractInfobyId(comboBox1.SelectedValue.ToString());
            }
        }

        private void btnClose_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, System.EventArgs e)
        {
            Accounting.Model.ContractFee contractFee = new Accounting.Model.ContractFee();
            contractFee.ActionDate = dateTimePicker1.Value.ToString("yyyyMMdd");

            contractFee.ActionType = comboBox2.SelectedItem.ToString();
            if (comboBox2.SelectedItem.ToString() == "收入")
            {
                contractFee.ActionFee = decimal.Parse(textBox2.Text);
            }
            else
            {
                contractFee.ActionFee = 0 - decimal.Parse(textBox2.Text);
            }
            contractFee.ContractID = int.Parse(comboBox1.SelectedValue.ToString());
            contractFee.Remark = textBox4.Text;

            cp010103.saveContractFee(contractFee);
            this.Close();
        }
    }
}